﻿// 项目：	Guan.CMS.Domain - Guan.CMS
// 文件名：	AddOrUpdateSiteConfigHandler.cs
// 
// 修改时间：2013-03-06 22:02
// 修改人：	Cooclsee

using System.Linq;
using Guan.CMS.CommandProcessor.Command;
using Guan.CMS.Core.Cache;
using Guan.CMS.Data.Infrastructure;
using Guan.CMS.Data.Repositories;
using Guan.CMS.Domain.Commands.SiteConfig;

namespace Guan.CMS.Domain.Handlers.SiteConfig
{
    public class AddOrUpdateSiteConfigHandler : ICommandHandler<AddOrUpdateSiteConfigCommand>
    {
        private readonly ISiteConfigRepository _siteConfigRepository;
        private readonly IUnitOfWork _unitOfWork;

        public AddOrUpdateSiteConfigHandler(ISiteConfigRepository siteConfigRepository, IUnitOfWork unitOfWork)
        {
            this._siteConfigRepository = siteConfigRepository;
            this._unitOfWork = unitOfWork;
        }

        public ICommandResult Execute(AddOrUpdateSiteConfigCommand command)
        {
            var model = _siteConfigRepository.GetList().FirstOrDefault();
            var isAdd = model == null;
            if (isAdd)
            {
                model = new Model.Entities.SiteConfig();
            }
            model.SiteName = command.SiteName;
            model.Footer = command.Footer;
            model.MetaKeywords = command.MetaKeywords;
            model.MetaDescription = command.MetaDescription;
            model.StatisticsCode = command.StatisticsCode;
            model.Address = command.Address;
            model.Beian = command.Beian;
            model.Email = command.Email;
            model.Mobile = command.Mobile;
            model.Tel = command.Tel;
            model.QQ = command.QQ;
            model.AboutUsSummary = command.AboutUsSummary;
            if (isAdd)
            {
                _siteConfigRepository.Add(model);
            }
            else
            {
                _siteConfigRepository.Update(model);
            }
            this._unitOfWork.Commit();
            HttpCacheExtension.Remove("SiteConfig");
            return new CommandResult(true);
        }
    }
}